package com.gues.service;

import com.gues.eneity.Article;
import com.gues.eneity.Tag;
import com.gues.eneity.Time;
import com.gues.mapper.ArticleMapper;
import com.gues.mapper.TagMapper;
import com.gues.mapper.TimeMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AdminService {

    @Resource
    ArticleMapper articleMapper;

    @Resource
    TagMapper tagMapper;

    @Resource
    TimeMapper timeMapper;

    public Integer insert(Article article) {

        // 加入其他选项
        Date date = new Date();
        article.setCreateDate(date);
        article.setViewNum(0);
        article.setDiggNum(0);
        int reault = articleMapper.insertArticle(article);

        // 拿到 id
        Integer aid = articleMapper.selectArticleId(article.getTitle());

        // tags
        List<Tag> tags = article.getTags();
        tags.forEach(item -> {
            item.setAid(aid);
            tagMapper.insert(item);

        });

        return reault;
    }

    public int insertTime(Time time) {
        Date date = new Date();
        time.setCreateDate(date);
        return timeMapper.insert(time);
    }

    public List<Article> selectList() {
        return articleMapper.selectAllArticle();
    }

    public int deleteById(Integer id) {
        return articleMapper.deleteById(id);
    }

}
